<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/6/2
 * Time: 9:26
 * 房产管理
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Property extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());

            if(isset($keyword)&&$keyword!="")
            {
                $map['a.house_name|uxr.room_name'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($xq_id)&&$xq_id!="")
            {
                $map['a.xq_id'] = $xq_id;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_house')->alias('a')
                ->join('up_xq ux','ux.xq_id =a.xq_id','left')
                ->join('up_xq_block uxb','uxb.block_id =a.block_id','left')
                ->join('up_xq_unit uxn','uxn.unit_id =a.unit_id','left')
                ->join('up_xq_room uxr','uxr.room_id =a.room_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_house')->alias('a')
                ->join('up_xq ux','ux.xq_id =a.xq_id','left')
                ->join('up_xq_block uxb','uxb.block_id =a.block_id','left')
                ->join('up_xq_unit uxn','uxn.unit_id =a.unit_id','left')
                ->join('up_xq_room uxr','uxr.room_id =a.room_id','left')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('a.house_id desc')
                ->field('a.*,ux.xq_name,uxb.block_name,uxn.unit_name,uxr.room_name')
                ->select();
            foreach ($lists as $key=>&$vo){
                $vo['name'] = $vo['block_name'].'-'.$vo['unit_name'].'-'.$vo['room_name'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $res = Db::table('up_xq')->field('xq_id,xq_name')->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function add(){
        if(Request::instance()->isPost()){
            $house_name =input('house_name');
            $house_num= input('house_num');
            $house_my_name = input('house_my_name');
            $house_my_phone = input('house_my_phone');
            $house_my_card = input('house_my_card');
            $house_bind_user = input('house_bind_user');
            if($house_bind_user==''){
                return json(['code' => 100, 'msg' => '绑定用户不能为空']);
            }
            $xq = input('xq');
            $block = input('block');
            $unit= input('unit');
            $room= input('room');
            $data['house_name'] = $house_name;
            $data['house_num'] = $house_num;
            $data['house_my_name'] = $house_my_name;
            $data['house_my_phone'] = $house_my_phone;
            $data['house_my_card'] = $house_my_card;
            $data['xq_id'] = $xq;
            $data['block_id'] = $block;
            $data['unit_id'] = $unit;
            $data['room_id'] = $room;
            $data['house_bind_user'] = $house_bind_user;
            $data['house_time'] = date('Y-m-d H:i:s',time());

            $res = Db::table('up_xq_house')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $res = Db::table('up_xq')->field('xq_id,xq_name')->select();
        $res1 = Db::table('up_u_user')->where('user_status',1)->field('user_id,user_phone,user_username')->select();
        $this->assign('res',$res);
        $this->assign('res1',$res1);
        return $this->fetch();
    }

    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $house_name =input('house_name');
            $house_num= input('house_num');
            $house_my_name = input('house_my_name');
            $house_my_phone = input('house_my_phone');
            $house_my_card = input('house_my_card');
            $data['house_name'] = $house_name;
            $data['house_num'] = $house_num;
            $data['house_my_name'] = $house_my_name;
            $data['house_my_phone'] = $house_my_phone;
            $data['house_my_card'] = $house_my_card;
            $res = Db::table('up_xq_house')->where('house_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.house_id');
        $res = Db::table('up_xq_house')->where('house_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function del(){
        $task_id = input('param.id');
        $res = Db::name('xq_house')->where('house_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function down(){
        $task_id = input('param.id');
        $data['house_bind_user'] = '';
        $data['is_bind'] = 2;
        $res = Db::name('xq_house')->where('house_id',$task_id)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function up(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $house_bind_user = input('house_bind_user');
            if($house_bind_user==''){
                return json(['code' => 100, 'msg' => '绑定用户不能为空']);
            }
            $data['house_bind_user'] = $house_bind_user;
            $data['is_bind'] = 1;
            $res = Db::table('up_xq_house')->where('house_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $house_id = input('param.house_id');
        $res1 = Db::table('up_u_user')->where('user_status',1)->field('user_id,user_phone,user_username')->select();
        $this->assign('res1',$res1);
        $this->assign('id',$house_id);
        return $this->fetch();
    }
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelIn(){
        set_time_limit(0);
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
            $time = date('Y-m-d H:i:s');
            Db::startTrans();
            try{
                for ($i=2;$i<=$highestRow;$i++) {
                    $b = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//获取B列的值
                    $c = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//获取C列的值
                    $d = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//获取D列的值
                    $e = $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//获取E列的值
                    $f = $objPHPExcel->getActiveSheet ()->getCell ("F" . $i)->getValue ();//获取E列的值
                    $g = $objPHPExcel->getActiveSheet ()->getCell ("G" . $i)->getValue ();//获取E列的值
                    $h = $objPHPExcel->getActiveSheet ()->getCell ("H" . $i)->getValue ();//获取E列的值
                    $ii = $objPHPExcel->getActiveSheet ()->getCell ("I" . $i)->getValue ();//获取E列的值
                    $j = $objPHPExcel->getActiveSheet ()->getCell ("J" . $i)->getValue ();//获取E列的值
                    $k = $objPHPExcel->getActiveSheet ()->getCell ("K" . $i)->getValue ();//获取E列的值
                    $data['xq_id'] = $b;
                    $data['block_id'] = $c;
                    $data['unit_id'] = $d;
                    $data['room_id'] = $e;
                    $data['house_name'] = $f;
                    $data['house_num'] = $g;
                    $data['house_my_name'] = $h;
                    $data['house_my_phone'] = $ii;
                    $data['house_my_card'] = $j;
                    $data['house_bind_user'] = $k;
                    if($data['house_bind_user']==''){
                        $data['is_bind'] =2;
                    }else{
                        $data['is_bind'] =1;
                    }
                    $data['house_time'] = $time;
                    Db::table('up_xq_house')->insertGetId($data);
                }
                Db::commit();
                echo '/admin/property/index';
            }catch(\Exception $e){
                Db::rollback();
            }

        }
        return $this->fetch('property/excelin');
    }
}